/*
 *  Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 *
 *  Licensed under the GNU General Public License v3 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl-3.0.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package xperiment.metaphor.model.notebook;

import xperiment.metaphor.model.DomainObject;
import xperiment.metaphor.model.Persistent;
import org.apache.commons.lang.builder.*;

import java.util.Date;

/**
 * A notebook page
 * @author Mark Ashworth <javameme@gmail.com>
 * @version 1.0.0
 * @since 1.0.0
 */
public class Notepage extends DomainObject implements Persistent, Comparable {

    private Long id;
    private Date added;
    private String body;
    private boolean enabled = true;

    public Notepage() {
        this(null, null);
    }

    public Notepage(String body) {
        this(body, new Date());
    }

    public Notepage(String body, Date added) {
        super();
        
        this.body = body;
        this.added = added;
    }

    public Long getId() {
        return id;
    }

    public Date getAdded() {
        return added;
    }

    public void setAdded(Date added) {
        Date old = getAdded();
        this.added = added;
        firePropertyChange("added", old, getAdded());
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        String old = getBody();
        this.body = body;
        firePropertyChange("body", old, getBody());
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        boolean old = isEnabled();
        this.enabled = enabled;
        firePropertyChange("enabled", old, isEnabled());
    }

    @Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (o == this) {
            return true;
        }
        if (o.getClass() != getClass()) {
            return false;
        }
        Notepage rhs = (Notepage) o;
        return new EqualsBuilder()
                .append(body, rhs.getBody())
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 45).append(body).toHashCode();
    }

    @Override
    public String toString() {
        return ToStringBuilder
                .reflectionToString(
                        this,
                        ToStringStyle.MULTI_LINE_STYLE,
                        true,
                        Notepage.class);
    }


    /**
     * The comparison is made on the date that the page was added to the
     * notebook.
     * @param o The other note page
     * @return int
     */
    public int compareTo(Object o) {
        Notepage rhs = (Notepage) o;
        return new CompareToBuilder()
                .append(added, rhs.getAdded())
                .toComparison();
    }
}
